<?php
/**
 * @datetime 15-1-14
 * ScriptCompressor.php
 */
namespace ulue\libs\compress\driver;

use ulue\libs\compress\AbstractCompress;

class ScriptCompressor extends AbstractCompress
{

    public function __construct()
    {
        parent::__construct();
    }

    public function suffix()
    {
        return 'js';
    }

    public function execute($files, $return=false)
    {
        $files = $this->checkFileExists($files,$this->getSuffix());

        ob_start();

        //包含你的全部js文档
        foreach ($files as $file)
        {
            include($file);
        }

        //得到buffer中的内容，即压缩后的js文件
        $this->output = ob_get_clean();
        $this->clearChar();

        if ($return) {
            return $this->output;
        }

        return $this;
    }

    public function pregRules()
    {
        return [
            'pattern' => [
                '!/\*[^*]*\*+([^/][^*]*\*+)*/!', //去除文件中的注释
                '/\/\/.*?[\r\n]/is', //去掉所有单行注释
                "/(?!\w)\s*?(?!\w)/is" // 多个空格 换成一个
            ],

            'replace' => ['','',''],
        ];
    }
}